package com.example.websocket.websocket.controller;

import com.example.websocket.websocket.pojo.*;
import com.example.websocket.websocket.service.MessageRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: websocket
 * @description:
 * @author: fks
 * @create: 2025-03-26 17:20
 **/
@RestController
@Slf4j
@RequestMapping("/message")
public class MessageRecordController {
    @Autowired
    private MessageRecordService messageRecordService;


    @PostMapping("/save")
    public void saveMessage(@RequestBody messageDTO messageDTO) {
        Message msg = new Message();
        msg.setToUser(messageDTO.getToUser());
        msg.setMessage(messageDTO.getMessage());
        String username = messageDTO.getFromUser();
        messageRecordService.saveMessage(msg,username);
    }

    /**
     * 获取消息
     * @param messageDTO,session
     * @return
     */
    @PostMapping("/getmessage")
    public Result getMessage(@RequestBody UserMsgDTO messageDTO, HttpSession session) {
        log.info("获取消息:{}",messageDTO);
        Result result = new Result();
        String fromUser = (String) session.getAttribute("username");
        List<MessageRecord> message = messageRecordService.getMessage(messageDTO.getToUser(),fromUser);
        result.setData(message);
        return result;

    }
}
